package de.uhlesoluschns.opensearch.ui;

import java.io.FileNotFoundException;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.LiveFolders;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import de.uhlesoluschns.opensearch.R;
import de.uhlesoluschns.opensearch.OpenSearch.Provider;
import de.uhlesoluschns.opensearch.OpenSearch.ProviderIcon;
import de.uhlesoluschns.opensearch.OpenSearch.ProviderLiveFolder;

public class ProviderList extends ListActivity {
	
	private static Intent createLiveFolder(Resources resources) {
		final Intent intent = new Intent();
		intent.setData(ProviderLiveFolder.CONTENT_URI);
		intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME, resources.getString(R.string.app_name));
		intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST);
		intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT, new Intent(Intent.ACTION_VIEW, Provider.CONTENT_URI));
		return intent;
	}
	
	private static class ProviderListAdapter extends CursorAdapter {
		private LayoutInflater inflater;
		
		public ProviderListAdapter(ProviderList providerList, Cursor cursor) {
			super(providerList, cursor, true);
			inflater = LayoutInflater.from(providerList);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			final ViewInfo info = (ViewInfo) view.getTag();
			
			info.id = cursor.getLong(info.idIndex);
			info.name.setText(cursor.getString(info.nameIndex));
			info.description.setText(cursor.getString(info.descriptionIndex));
			
			byte [] imageData = cursor.getBlob(info.iconIndex);
			
			info.icon.setImageBitmap(BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = inflater.inflate(R.layout.provider_list, parent, false);
			
			final ViewInfo info = new ViewInfo();
			info.name = (TextView) view.findViewById(R.id.provider_list_name);
			info.description = (TextView) view.findViewById(R.id.provider_list_description);
			info.icon = (ImageView) view.findViewById(R.id.provider_list_icon);
			
			info.idIndex = cursor.getColumnIndexOrThrow(Provider.COL_ID);
			info.nameIndex = cursor.getColumnIndexOrThrow(Provider.COL_SHORT_NAME);
			info.descriptionIndex = cursor.getColumnIndexOrThrow(Provider.COL_DESCRIPTION);
			info.iconIndex = cursor.getColumnIndexOrThrow(Provider.COL_ICON);
			
			view.setTag(info);
			return view;
		}
		
		private static class ViewInfo {
			long id;
			TextView name;
			TextView description;
			ImageView icon;
			
			int idIndex;
			int nameIndex;
			int descriptionIndex;
			int iconIndex;
		}
		
	}
	
	private Cursor providers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final Intent intent = getIntent();
		final String action = intent.getAction();
		
		if(LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
			setResult(RESULT_OK, createLiveFolder(getResources()));
			finish();
		} else if(Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			providers = managedQuery(Provider.CONTENT_URI, new String[] {
					Provider.COL_ID, Provider.COL_SHORT_NAME, Provider.COL_DESCRIPTION, Provider.COL_ICON},
					null, null, Provider.COL_SHORT_NAME);
			setListAdapter(new ProviderListAdapter(this, providers));
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		providers.moveToPosition(position);

		final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(Provider.CONTENT_URI, providers.getString(providers.getColumnIndex(Provider.COL_ID))));
		
        final Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, providers.getString(providers.getColumnIndex(Provider.COL_SHORT_NAME)));
        
        Bitmap combinedIcon = null;
		try {
			combinedIcon = BitmapFactory.decodeStream(openFileInput(
					ProviderIcon.getPath(providers.getInt(providers.getColumnIndex(Provider.COL_ID)))));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        
        
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, combinedIcon);
        setResult(RESULT_OK, intent);
        finish();
	}
}
